import pymcws,pymcwsapi
import asyncio

ws=None
api=pymcwsapi.MCWSApi()

async def onconnect():
    global ws,api
    print("[信息]","已连接到Minecraft「客户端」")
    await ws.sendcommand("/title @s actionbar §b§lPyMCWS By ImFatFish")
    await ws.sendcommand(tellraw("§e[信息] 已连接到PyMCWS「服务端」"))

async def ondisconnect():
    global ws,api
    print("[警告]","已断开Minecraft「客户端」的连接,如果您是1.14.x,请重启游戏再尝试连接")

async def onreceive(recv_data):
    global ws,api
    data=api.get_pkg_data(recv_data)
    if api.is_playerchat(recv_data):
        print("<%s> %s" % (data[0],data[1]))
        if data[1].startswith("./"):
            await ws.sendcommand(data[1][1:len(data[1])])
    elif api.is_playersay(recv_data):
        print(data[1])
    elif api.is_block_broken(recv_data):
        print("*%s(%s)方块被物品id:%s破坏了*" % (data[0],data[1],data[2]))
    elif api.is_block_placed(recv_data):
        print("*%s(%s)方块被放置了*" % (data[0],data[1]))
    elif api.is_command_response(recv_data):
        if data[1] != None and data[1] != "":
            print(data[1])
            await ws.sendcommand(tellraw(data[1]))

def tellraw(text):
    return '/tellraw @s {"rawtext":[{"text":"%s"}]}' % text

ws=pymcws.MCWS(ws_onconnect=onconnect,ws_ondisconnect=ondisconnect,ws_onreceive=onreceive)
ws.start_server()